﻿// Decompiled with JetBrains decompiler
// Type: XamlEditor.ResizingAdorner
// Assembly: XamlEditor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 2AF0471B-7BFE-4738-893B-A4F91E243353
// Assembly location: C:\Users\KasimGulcan\Desktop\Kosgeb\DynaCAD\Project\lib\XamlEditor.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace XamlEditor
{
  public class ResizingAdorner : Adorner
  {
    private List<Thumb> PathPoints = new List<Thumb>();
    private Point? InitialPosition = new Point?();
    private double ScaleXC = 1.0;
    private double ScaleYC = 1.0;
    private double ScaleX = 1.0;
    private double ScaleY = 1.0;
    private Thumb topLeft;
    private Thumb topRight;
    private Thumb bottomLeft;
    private Thumb bottomRight;
    private int TN;
    private int CN;
    private int ThumbNumber;
    private Point CurrentPosition;
    private double SetLeft;
    private double SetTop;
    private VisualCollection visualChildren;

    protected override int VisualChildrenCount
    {
      get
      {
        return this.visualChildren.Count;
      }
    }

    public ResizingAdorner(UIElement adornedElement)
      : base(adornedElement)
    {
      this.visualChildren = new VisualCollection((Visual) this);
      this.PathPointsThumbHandler(this.AdornedElement as Path);
      this.BuildAdornerPathPoints(this.PathPoints);
      this.BuildAdornerCorner(ref this.topLeft, Cursors.ScrollAll);
      this.BuildAdornerCorner(ref this.topRight, Cursors.ScrollAll);
      this.BuildAdornerCorner(ref this.bottomLeft, Cursors.ScrollAll);
      this.BuildAdornerCorner(ref this.bottomRight, Cursors.ScrollAll);
      this.bottomLeft.DragDelta += new DragDeltaEventHandler(this.HandleBottomLeft);
      this.bottomRight.DragDelta += new DragDeltaEventHandler(this.HandleBottomRight);
      this.bottomRight.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.bottomRight_PreviewMouseLeftButtonDown);
      this.topLeft.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.topLeft_PreviewMouseLeftButtonDown);
      this.topLeft.DragDelta += new DragDeltaEventHandler(this.HandleTopLeft);
      this.topRight.DragDelta += new DragDeltaEventHandler(this.HandleTopRight);
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
      Rect rect = new Rect(this.AdornedElement.DesiredSize);
      Path path = this.AdornedElement as Path;
      SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.Green);
      solidColorBrush.Opacity = 0.3;
      Pen pen = new Pen((Brush) solidColorBrush, 1.5);
      drawingContext.DrawRectangle((Brush) null, pen, new Rect(path.Data.Bounds.TopLeft, path.Data.Bounds.BottomRight));
    }

    private void Position_MouseMove()
    {
      this.CurrentPosition = Mouse.GetPosition((IInputElement) ((FrameworkElement) this.AdornedElement).Parent);
    }

    private void PathPoints_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      this.InitialPosition = new Point?(e.GetPosition((IInputElement) ((FrameworkElement) this.AdornedElement).Parent));
    }

    private void PathPoints_DragDelta(object sender, DragDeltaEventArgs e)
    {
      this.CN = 0;
      this.Position_MouseMove();
      Thumb thumb = sender as Thumb;
      Path path = this.AdornedElement as Path;
      this.TN = Convert.ToInt32(thumb.Name.Replace("A", ""));
      if (path.Data is LineGeometry)
        this.HandleLineGeometry((LineGeometry) path.Data);
      else if (path.Data is RectangleGeometry)
        this.HandleRectangleGeometry((RectangleGeometry) path.Data);
      else if (path.Data is EllipseGeometry)
        this.HandleEllipseGeometry((EllipseGeometry) path.Data);
      else if (path.Data is GeometryGroup)
        this.HandleGeometryGroup((GeometryGroup) path.Data);
      else if (path.Data is PathGeometry)
        this.HandlePathGeometry((PathGeometry) path.Data);
      else if (path.Data is CombinedGeometry)
        this.HandleCombinedGeometry((CombinedGeometry) path.Data);
      this.InvalidateVisual();
    }

    private void HandleCombinedGeometry(CombinedGeometry CG)
    {
      foreach (Geometry geometry in new List<Geometry>()
      {
        CG.Geometry1,
        CG.Geometry2
      })
      {
        if (geometry is LineGeometry)
          this.HandleLineGeometry((LineGeometry) geometry);
        else if (geometry is RectangleGeometry)
          this.HandleRectangleGeometry((RectangleGeometry) geometry);
        else if (geometry is EllipseGeometry)
          this.HandleEllipseGeometry((EllipseGeometry) geometry);
        else if (geometry is PathGeometry)
          this.HandlePathGeometry((PathGeometry) geometry);
        else if (geometry is GeometryGroup)
          this.HandleGeometryGroup((GeometryGroup) geometry);
        else if (geometry is CombinedGeometry)
          this.HandleCombinedGeometry((CombinedGeometry) geometry);
      }
    }

    private void HandleGeometryGroup(GeometryGroup GG)
    {
      foreach (Geometry geometry in Enumerable.ToList<Geometry>((IEnumerable<Geometry>) GG.Children))
      {
        if (geometry is LineGeometry)
          this.HandleLineGeometry((LineGeometry) geometry);
        else if (geometry is RectangleGeometry)
          this.HandleRectangleGeometry((RectangleGeometry) geometry);
        else if (geometry is EllipseGeometry)
          this.HandleEllipseGeometry((EllipseGeometry) geometry);
        else if (geometry is PathGeometry)
          this.HandlePathGeometry((PathGeometry) geometry);
        else if (geometry is GeometryGroup)
          this.HandleGeometryGroup((GeometryGroup) geometry);
        else if (geometry is CombinedGeometry)
          this.HandleCombinedGeometry((CombinedGeometry) geometry);
      }
    }

    private void HandlePathGeometry(PathGeometry PG)
    {
      this.InvalidateVisual();
      foreach (PathFigure PF in Enumerable.ToList<PathFigure>((IEnumerable<PathFigure>) PG.Figures))
      {
        if (this.CN == this.TN)
        {
          this.HandlePathFigureThumb(PF);
          break;
        }
        this.CN = this.CN + 1;
        foreach (PathSegment pathSegment in Enumerable.ToList<PathSegment>((IEnumerable<PathSegment>) PF.Segments))
        {
          if (pathSegment is LineSegment)
          {
            if (this.CN == this.TN)
            {
              this.HandleLineSegmentThumb((LineSegment) pathSegment);
              return;
            }
            this.CN = this.CN + 1;
          }
          else if (pathSegment is ArcSegment)
          {
            if (this.CN == this.TN)
            {
              this.HandleArcSegmentThumb((ArcSegment) pathSegment);
              return;
            }
            this.CN = this.CN + 1;
          }
          else if (pathSegment is PolyLineSegment)
          {
            if (this.CN == this.TN || this.CN + ((PolyLineSegment) pathSegment).Points.Count > this.TN)
            {
              this.HandlePolyLineSegment((PolyLineSegment) pathSegment);
              return;
            }
            this.CN = this.CN + ((PolyLineSegment) pathSegment).Points.Count;
          }
          else if (pathSegment is BezierSegment)
          {
            if (this.CN == this.TN || this.CN + 3 > this.TN)
            {
              this.HandleBezierSegment((BezierSegment) pathSegment);
              return;
            }
            this.CN = this.CN + 3;
          }
          else if (pathSegment is QuadraticBezierSegment)
          {
            if (this.CN == this.TN || this.CN + 2 > this.TN)
            {
              this.HandleQuadraticBezierSegment((QuadraticBezierSegment) pathSegment);
              return;
            }
            this.CN = this.CN + 2;
          }
          else if (pathSegment is PolyQuadraticBezierSegment)
          {
            if (this.CN == this.TN || this.CN + ((PolyQuadraticBezierSegment) pathSegment).Points.Count > this.TN)
            {
              this.HandlePolyQuadraticBezierSegment((PolyQuadraticBezierSegment) pathSegment);
              return;
            }
            this.CN = this.CN + ((PolyQuadraticBezierSegment) pathSegment).Points.Count;
          }
          else if (pathSegment is PolyBezierSegment)
          {
            if (this.CN == this.TN || this.CN + ((PolyBezierSegment) pathSegment).Points.Count > this.TN)
            {
              this.HandlePolyBezierSegment((PolyBezierSegment) pathSegment);
              return;
            }
            this.CN = this.CN + ((PolyBezierSegment) pathSegment).Points.Count;
          }
        }
      }
    }

    private void HandleQuadraticBezierSegment(QuadraticBezierSegment QBS)
    {
      if (this.TN - this.CN == 0)
      {
        QBS.Point1 = new Point(QBS.Point1.X + this.CurrentPosition.X - this.InitialPosition.Value.X, QBS.Point1.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
        this.InitialPosition = new Point?(this.CurrentPosition);
        this.InvalidateVisual();
      }
      else
      {
        if (this.TN - this.CN != 1)
          return;
        QBS.Point2 = new Point(QBS.Point2.X + this.CurrentPosition.X - this.InitialPosition.Value.X, QBS.Point2.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
        this.InitialPosition = new Point?(this.CurrentPosition);
        this.InvalidateVisual();
      }
    }

    private void HandleBezierSegment(BezierSegment BS)
    {
      if (this.TN - this.CN == 0)
      {
        BS.Point1 = new Point(BS.Point1.X + this.CurrentPosition.X - this.InitialPosition.Value.X, BS.Point1.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
        this.InitialPosition = new Point?(this.CurrentPosition);
        this.InvalidateVisual();
      }
      else if (this.TN - this.CN == 1)
      {
        BS.Point2 = new Point(BS.Point2.X + this.CurrentPosition.X - this.InitialPosition.Value.X, BS.Point2.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
        this.InitialPosition = new Point?(this.CurrentPosition);
        this.InvalidateVisual();
      }
      else
      {
        if (this.TN - this.CN != 2)
          return;
        BS.Point3 = new Point(BS.Point3.X + this.CurrentPosition.X - this.InitialPosition.Value.X, BS.Point3.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
        this.InitialPosition = new Point?(this.CurrentPosition);
        this.InvalidateVisual();
      }
    }

    private void HandlePolyBezierSegment(PolyBezierSegment PBS)
    {
      PBS.Points[this.TN - this.CN] = new Point(PBS.Points[this.TN - this.CN].X + this.CurrentPosition.X - this.InitialPosition.Value.X, PBS.Points[this.TN - this.CN].Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
      this.InitialPosition = new Point?(this.CurrentPosition);
      this.UpdateLayout();
      this.InvalidateVisual();
    }

    private void HandlePolyQuadraticBezierSegment(PolyQuadraticBezierSegment PQBS)
    {
      PQBS.Points[this.TN - this.CN] = new Point(PQBS.Points[this.TN - this.CN].X + this.CurrentPosition.X - this.InitialPosition.Value.X, PQBS.Points[this.TN - this.CN].Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
      this.InitialPosition = new Point?(this.CurrentPosition);
      this.UpdateLayout();
      this.InvalidateVisual();
    }

    private void HandlePolyLineSegment(PolyLineSegment PLS)
    {
      PLS.Points[this.TN - this.CN] = new Point(PLS.Points[this.TN - this.CN].X + this.CurrentPosition.X - this.InitialPosition.Value.X, PLS.Points[this.TN - this.CN].Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
      this.InitialPosition = new Point?(this.CurrentPosition);
      this.UpdateLayout();
      this.InvalidateVisual();
    }

    private void HandleArcSegmentThumb(ArcSegment AS)
    {
      AS.Point = new Point(AS.Point.X + this.CurrentPosition.X - this.InitialPosition.Value.X, AS.Point.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
      this.InitialPosition = new Point?(this.CurrentPosition);
    }

    private void HandleLineSegmentThumb(LineSegment LS)
    {
      LS.Point = new Point(LS.Point.X + this.CurrentPosition.X - this.InitialPosition.Value.X, LS.Point.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
      this.InitialPosition = new Point?(this.CurrentPosition);
    }

    private void HandlePathFigureThumb(PathFigure PF)
    {
      PF.StartPoint = new Point(PF.StartPoint.X + this.CurrentPosition.X - this.InitialPosition.Value.X, PF.StartPoint.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
      this.InitialPosition = new Point?(this.CurrentPosition);
    }

    private void HandleLineGeometry(LineGeometry LG)
    {
      if (this.CN + 2 > this.TN)
      {
        if (this.CN + 2 - this.TN == 2)
          LG.StartPoint = new Point(LG.StartPoint.X + this.CurrentPosition.X - this.InitialPosition.Value.X, LG.StartPoint.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
        if (this.CN + 2 - this.TN == 1)
          LG.EndPoint = new Point(LG.EndPoint.X + this.CurrentPosition.X - this.InitialPosition.Value.X, LG.EndPoint.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y);
        this.InitialPosition = new Point?(this.CurrentPosition);
      }
      else
        this.CN = this.CN + 2;
    }

    private void HandleEllipseGeometry(EllipseGeometry EG)
    {
      if (this.CN + 3 > this.TN)
      {
        if (this.TN - this.CN == 0)
          EG.RadiusY = EG.RadiusY - this.CurrentPosition.Y + this.InitialPosition.Value.Y;
        else if (this.TN - this.CN == 1)
          EG.RadiusX = EG.RadiusX + this.CurrentPosition.X - this.InitialPosition.Value.X;
        else if (this.TN - this.CN == 2)
        {
          double x = EG.Center.X + this.CurrentPosition.X - this.InitialPosition.Value.X;
          double y = EG.Center.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y;
          EG.Center = new Point(x, y);
        }
        this.InitialPosition = new Point?(this.CurrentPosition);
      }
      else
        this.CN = this.CN + 3;
    }

    private void HandleRectangleGeometry(RectangleGeometry RG)
    {
      if (this.CN + 2 > this.TN)
      {
        if (this.TN - this.CN == 0)
          RG.Rect = new Rect(new Point(RG.Rect.TopLeft.X + this.CurrentPosition.X - this.InitialPosition.Value.X, RG.Rect.TopLeft.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y), new Point(RG.Rect.BottomRight.X, RG.Rect.BottomRight.Y));
        else if (this.TN - this.CN == 1)
          RG.Rect = new Rect(new Point(RG.Rect.TopLeft.X, RG.Rect.TopLeft.Y), new Point(RG.Rect.BottomRight.X + this.CurrentPosition.X - this.InitialPosition.Value.X, RG.Rect.BottomRight.Y + this.CurrentPosition.Y - this.InitialPosition.Value.Y));
        this.InitialPosition = new Point?(this.CurrentPosition);
      }
      else
        this.CN = this.CN + 2;
    }

    private void topLeft_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      this.InitialPosition = new Point?(e.GetPosition((IInputElement) ((FrameworkElement) this.AdornedElement).Parent));
      this.SetLeft = !double.IsNaN(Canvas.GetLeft(this.AdornedElement)) ? Canvas.GetLeft(this.AdornedElement) : 0.0;
      if (double.IsNaN(Canvas.GetTop(this.AdornedElement)))
        this.SetTop = 0.0;
      else
        this.SetTop = Canvas.GetTop(this.AdornedElement);
    }

    private void HandleTopLeft(object sender, DragDeltaEventArgs args)
    {
      if (!(this.AdornedElement is FrameworkElement) || !(sender is Thumb))
        return;
      this.Position_MouseMove();
      Canvas.SetLeft(this.AdornedElement, this.SetLeft + (this.CurrentPosition.X - this.InitialPosition.Value.X));
      Canvas.SetTop(this.AdornedElement, this.SetTop + (this.CurrentPosition.Y - this.InitialPosition.Value.Y));
    }

    private void bottomRight_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      Path path = this.AdornedElement as Path;
      this.InitialPosition = new Point?(e.GetPosition((IInputElement) ((FrameworkElement) this.AdornedElement).Parent));
      if (path.RenderTransform is TransformGroup)
      {
        if (Enumerable.Count<ScaleTransform>(Enumerable.OfType<ScaleTransform>((IEnumerable) ((TransformGroup) path.RenderTransform).Children)) > 0)
        {
          ScaleTransform scaleTransform = Enumerable.First<ScaleTransform>(Enumerable.OfType<ScaleTransform>((IEnumerable) ((TransformGroup) path.RenderTransform).Children));
          this.ScaleX = scaleTransform.ScaleX;
          this.ScaleY = scaleTransform.ScaleY;
        }
        else
        {
          this.ScaleX = 1.0;
          this.ScaleY = 1.0;
        }
      }
      else
      {
        this.ScaleX = 1.0;
        this.ScaleY = 1.0;
      }
    }

    private void HandleBottomRight(object sender, DragDeltaEventArgs args)
    {
      Path path = this.AdornedElement as Path;
      Thumb thumb = sender as Thumb;
      if (path == null || thumb == null)
        return;
      DependencyObject parent = path.Parent;
      this.Position_MouseMove();
      if (path.RenderTransform is TransformGroup)
      {
        if (Enumerable.Count<ScaleTransform>(Enumerable.OfType<ScaleTransform>((IEnumerable) ((TransformGroup) path.RenderTransform).Children)) > 0)
        {
          ScaleTransform scaleTransform = Enumerable.First<ScaleTransform>(Enumerable.OfType<ScaleTransform>((IEnumerable) ((TransformGroup) path.RenderTransform).Children));
          scaleTransform.ScaleX = this.ScaleX * ((path.Data.Bounds.BottomRight.X - path.Data.Bounds.TopLeft.X + (this.CurrentPosition.X - this.InitialPosition.Value.X) / this.ScaleX) / (path.Data.Bounds.BottomRight.X - path.Data.Bounds.TopLeft.X));
          scaleTransform.ScaleY = this.ScaleY * (path.Data.Bounds.BottomRight.Y - path.Data.Bounds.TopLeft.Y + (this.CurrentPosition.Y - this.InitialPosition.Value.Y) / this.ScaleY) / (path.Data.Bounds.BottomRight.Y - path.Data.Bounds.TopLeft.Y);
          scaleTransform.CenterX = path.Data.Bounds.TopLeft.X;
          scaleTransform.CenterY = path.Data.Bounds.TopLeft.Y;
          this.ScaleXC = scaleTransform.ScaleX;
          this.ScaleYC = scaleTransform.ScaleY;
          this.InvalidateVisual();
        }
        else
          ((TransformGroup) path.RenderTransform).Children.Add((Transform) new ScaleTransform()
          {
            ScaleX = ((path.Data.Bounds.Right + (this.CurrentPosition.X - this.InitialPosition.Value.X)) / path.Data.Bounds.Right),
            ScaleY = ((path.Data.Bounds.Bottom + (this.CurrentPosition.Y - this.InitialPosition.Value.Y)) / path.Data.Bounds.Bottom)
          });
      }
      else
        path.RenderTransform = (Transform) new TransformGroup()
        {
          Children = {
            (Transform) new ScaleTransform()
            {
              ScaleX = ((path.Data.Bounds.Right + (this.CurrentPosition.X - this.InitialPosition.Value.X)) / path.Data.Bounds.Right),
              ScaleY = ((path.Data.Bounds.Bottom + (this.CurrentPosition.Y - this.InitialPosition.Value.Y)) / path.Data.Bounds.Bottom)
            }
          }
        };
    }

    private void HandleTopRight(object sender, DragDeltaEventArgs args)
    {
      UIElement adornedElement = this.AdornedElement;
    }

    private void HandleBottomLeft(object sender, DragDeltaEventArgs args)
    {
      UIElement adornedElement = this.AdornedElement;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
      Path path = this.AdornedElement as Path;
      double x1 = path.Data.Bounds.BottomRight.X;
      double y1 = path.Data.Bounds.BottomRight.Y;
      double x2 = path.Data.Bounds.TopLeft.X;
      double y2 = path.Data.Bounds.TopLeft.Y;
      this.ArrangePathPointsThumbs(this.PathPoints);
      this.topLeft.Arrange(new Rect(new Point(x2, y2), new Point(x1, y1)));
      return finalSize;
    }

    private void ArrangePathPointsThumbs(List<Thumb> a)
    {
      Path path = this.AdornedElement as Path;
      if (path.Data is LineGeometry)
      {
        this.ThumbNumber = 0;
        this.ArrangeLineGeometryThumbs((LineGeometry) path.Data, this.ThumbNumber);
      }
      else if (path.Data is RectangleGeometry)
      {
        this.ThumbNumber = 0;
        this.ArrangeRectangleGeometryThumbs((RectangleGeometry) path.Data, this.ThumbNumber);
      }
      else if (path.Data is EllipseGeometry)
      {
        this.ThumbNumber = 0;
        this.ArrangeEllipseGeometryThumbs((EllipseGeometry) path.Data, this.ThumbNumber);
      }
      else if (path.Data is GeometryGroup)
      {
        this.ThumbNumber = 0;
        this.ArrangeGeometryGroupThumbs((GeometryGroup) path.Data);
      }
      else if (path.Data is PathGeometry)
      {
        this.ThumbNumber = 0;
        this.ArrangePathGeometryThumbs((PathGeometry) path.Data);
      }
      else
      {
        if (!(path.Data is CombinedGeometry))
          return;
        this.ThumbNumber = 0;
        this.ArrangeCombinedGeometryThumbs((CombinedGeometry) path.Data);
      }
    }

    private void ArrangeCombinedGeometryThumbs(CombinedGeometry CG)
    {
      foreach (Geometry geometry in new List<Geometry>()
      {
        CG.Geometry1,
        CG.Geometry2
      })
      {
        if (geometry is LineGeometry)
          this.ArrangeLineGeometryThumbs((LineGeometry) geometry, this.ThumbNumber);
        else if (geometry is PathGeometry)
          this.ArrangePathGeometryThumbs((PathGeometry) geometry);
        else if (geometry is RectangleGeometry)
          this.ArrangeRectangleGeometryThumbs((RectangleGeometry) geometry, this.ThumbNumber);
        else if (geometry is EllipseGeometry)
          this.ArrangeEllipseGeometryThumbs((EllipseGeometry) geometry, this.ThumbNumber);
        else if (geometry is GeometryGroup)
          this.ArrangeGeometryGroupThumbs((GeometryGroup) geometry);
        else if (geometry is CombinedGeometry)
          this.ArrangeCombinedGeometryThumbs((CombinedGeometry) geometry);
      }
    }

    private void ArrangeEllipseGeometryThumbs(EllipseGeometry EG, int i)
    {
      this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point((EG.Bounds.TopLeft.X + Math.Abs(EG.RadiusX)) * 2.0 + 40.0, EG.Bounds.Top * 2.0 + 40.0)));
      this.PathPoints[i + 1].Arrange(new Rect(new Point(-40.0, -40.0), new Point(EG.Bounds.Right * 2.0 + 40.0, (EG.Bounds.Top + Math.Abs(EG.RadiusY)) * 2.0 + 40.0)));
      this.PathPoints[i + 2].Arrange(new Rect(new Point(-40.0, -40.0), new Point(EG.Bounds.TopLeft.X + EG.Bounds.BottomRight.X + 40.0, EG.Bounds.TopLeft.Y + EG.Bounds.BottomRight.Y + 40.0)));
      this.ThumbNumber = this.ThumbNumber + 3;
    }

    private void ArrangeRectangleGeometryThumbs(RectangleGeometry RG, int i)
    {
      this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(RG.Rect.TopLeft.X * 2.0 + 40.0, RG.Rect.TopLeft.Y * 2.0 + 40.0)));
      this.PathPoints[i + 1].Arrange(new Rect(new Point(-40.0, -40.0), new Point(RG.Rect.BottomRight.X * 2.0 + 40.0, RG.Rect.BottomRight.Y * 2.0 + 40.0)));
      this.ThumbNumber = this.ThumbNumber + 2;
    }

    private void ArrangeLineGeometryThumbs(LineGeometry a, int i)
    {
      this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(a.StartPoint.X * 2.0 + 40.0, a.StartPoint.Y * 2.0 + 40.0)));
      this.PathPoints[i + 1].Arrange(new Rect(new Point(-40.0, -40.0), new Point(a.EndPoint.X * 2.0 + 40.0, a.EndPoint.Y * 2.0 + 40.0)));
      this.ThumbNumber = this.ThumbNumber + 2;
    }

    private void ArrangePathGeometryThumbs(PathGeometry PG)
    {
      foreach (PathFigure PF in Enumerable.ToList<PathFigure>((IEnumerable<PathFigure>) PG.Figures))
      {
        this.ArrangePathFigureThumbs(PF, this.ThumbNumber);
        this.ThumbNumber = this.ThumbNumber + 1;
      }
    }

    private void ArrangePathFigureThumbs(PathFigure PF, int i)
    {
      this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(PF.StartPoint.X * 2.0 + 40.0, PF.StartPoint.Y * 2.0 + 40.0)));
      foreach (PathSegment pathSegment in Enumerable.ToList<PathSegment>((IEnumerable<PathSegment>) PF.Segments))
      {
        if (pathSegment is LineSegment)
        {
          this.ThumbNumber = this.ThumbNumber + 1;
          this.ArrangeLineSegmentThumbs((LineSegment) pathSegment, this.ThumbNumber);
        }
        else if (pathSegment is ArcSegment)
        {
          this.ThumbNumber = this.ThumbNumber + 1;
          this.ArrangeArcSegmentThumbs((ArcSegment) pathSegment, this.ThumbNumber);
        }
        else if (pathSegment is PolyLineSegment)
        {
          this.ThumbNumber = this.ThumbNumber + ((PolyLineSegment) pathSegment).Points.Count;
          this.ArrangePolyLineSegment((PolyLineSegment) pathSegment, this.ThumbNumber);
        }
        else if (pathSegment is BezierSegment)
        {
          this.ThumbNumber = this.ThumbNumber + 3;
          this.ArrangeBezierSegmentThumbs((BezierSegment) pathSegment, this.ThumbNumber);
        }
        else if (pathSegment is QuadraticBezierSegment)
        {
          this.ThumbNumber = this.ThumbNumber + 2;
          this.ArrangeQuadraticBezierSegmentThumbs((QuadraticBezierSegment) pathSegment, this.ThumbNumber);
        }
        else if (pathSegment is PolyBezierSegment)
        {
          this.ThumbNumber = this.ThumbNumber + ((PolyBezierSegment) pathSegment).Points.Count;
          this.ArrangePolyBezierSegmentThumbs((PolyBezierSegment) pathSegment, this.ThumbNumber);
        }
        else if (pathSegment is PolyQuadraticBezierSegment)
        {
          this.ThumbNumber = this.ThumbNumber + ((PolyBezierSegment) pathSegment).Points.Count;
          this.ArrangePolyQuadraticBezierSegmentThumbs((PolyQuadraticBezierSegment) pathSegment, this.ThumbNumber);
        }
      }
    }

    private void ArrangeQuadraticBezierSegmentThumbs(QuadraticBezierSegment QBS, int i)
    {
      this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(QBS.Point2.X * 2.0 + 40.0, QBS.Point2.Y * 2.0 + 40.0)));
      this.PathPoints[i - 1].Arrange(new Rect(new Point(-40.0, -40.0), new Point(QBS.Point1.X * 2.0 + 40.0, QBS.Point1.Y * 2.0 + 40.0)));
    }

    private void ArrangePolyBezierSegmentThumbs(PolyBezierSegment PBS, int i)
    {
      for (int index = 0; index < PBS.Points.Count; ++index)
      {
        this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(PBS.Points[PBS.Points.Count - 1 - index].X * 2.0 + 40.0, PBS.Points[PBS.Points.Count - 1 - index].Y * 2.0 + 40.0)));
        --i;
      }
    }

    private void ArrangePolyQuadraticBezierSegmentThumbs(PolyQuadraticBezierSegment PQBS, int i)
    {
      for (int index = 0; index < PQBS.Points.Count; ++index)
      {
        this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(PQBS.Points[PQBS.Points.Count - 1 - index].X * 2.0 + 40.0, PQBS.Points[PQBS.Points.Count - 1 - index].Y * 2.0 + 40.0)));
        --i;
      }
    }

    private void ArrangePolyLineSegment(PolyLineSegment PLS, int i)
    {
      for (int index = 0; index < PLS.Points.Count; ++index)
      {
        this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(PLS.Points[PLS.Points.Count - 1 - index].X * 2.0 + 40.0, PLS.Points[PLS.Points.Count - 1 - index].Y * 2.0 + 40.0)));
        --i;
      }
    }

    private void ArrangeArcSegmentThumbs(ArcSegment AS, int i)
    {
      this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(AS.Point.X * 2.0 + 40.0, AS.Point.Y * 2.0 + 40.0)));
    }

    private void ArrangeLineSegmentThumbs(LineSegment LS, int i)
    {
      this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(LS.Point.X * 2.0 + 40.0, LS.Point.Y * 2.0 + 40.0)));
    }

    private void ArrangeBezierSegmentThumbs(BezierSegment BS, int i)
    {
      this.PathPoints[i].Arrange(new Rect(new Point(-40.0, -40.0), new Point(BS.Point3.X * 2.0 + 40.0, BS.Point3.Y * 2.0 + 40.0)));
      this.PathPoints[i - 1].Arrange(new Rect(new Point(-40.0, -40.0), new Point(BS.Point2.X * 2.0 + 40.0, BS.Point2.Y * 2.0 + 40.0)));
      this.PathPoints[i - 2].Arrange(new Rect(new Point(-40.0, -40.0), new Point(BS.Point1.X * 2.0 + 40.0, BS.Point1.Y * 2.0 + 40.0)));
    }

    private void ArrangeGeometryGroupThumbs(GeometryGroup GG)
    {
      foreach (Geometry geometry in Enumerable.ToList<Geometry>((IEnumerable<Geometry>) GG.Children))
      {
        if (geometry is LineGeometry)
          this.ArrangeLineGeometryThumbs((LineGeometry) geometry, this.ThumbNumber);
        else if (geometry is PathGeometry)
          this.ArrangePathGeometryThumbs((PathGeometry) geometry);
        else if (geometry is RectangleGeometry)
          this.ArrangeRectangleGeometryThumbs((RectangleGeometry) geometry, this.ThumbNumber);
        else if (geometry is EllipseGeometry)
          this.ArrangeEllipseGeometryThumbs((EllipseGeometry) geometry, this.ThumbNumber);
        else if (geometry is GeometryGroup)
          this.ArrangeGeometryGroupThumbs((GeometryGroup) geometry);
        else if (geometry is CombinedGeometry)
          this.ArrangeCombinedGeometryThumbs((CombinedGeometry) geometry);
      }
    }

    private void BuildAdornerCorner(ref Thumb cornerThumb, Cursor customizedCursor)
    {
      UIElement adornedElement = this.AdornedElement;
      if (cornerThumb != null)
        return;
      cornerThumb = new Thumb();
      cornerThumb.Cursor = customizedCursor;
      cornerThumb.Height = cornerThumb.Width = 10.0;
      cornerThumb.Opacity = 1.0;
      cornerThumb.Background = (Brush) new SolidColorBrush(Colors.Black);
      this.visualChildren.Add((Visual) cornerThumb);
    }

    private void BuildAdornerPathPoints(List<Thumb> a)
    {
      foreach (Thumb thumb in a)
      {
        thumb.Cursor = Cursors.Pen;
        thumb.Style = (Style) this.FindResource((object) "ThumbStyle");
        this.visualChildren.Add((Visual) thumb);
      }
    }

    public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
    {
      UIElement adornedElement = this.AdornedElement;
      if (this.visualChildren != null)
      {
        foreach (Thumb thumb in Enumerable.OfType<Thumb>((IEnumerable) this.visualChildren))
        {
          thumb.RenderTransform = (Transform) new ScaleTransform(1.0 / this.ScaleXC, 1.0 / this.ScaleYC);
          thumb.RenderTransformOrigin = new Point(0.5, 0.5);
        }
      }
      return base.GetDesiredTransform(transform);
    }

    protected override Visual GetVisualChild(int index)
    {
      return this.visualChildren[index];
    }

    protected void PathPointsThumbHandler(Path a)
    {
      if (a.Data is LineGeometry)
        this.LineGeometryThumbHandler((LineGeometry) a.Data);
      else if (a.Data is RectangleGeometry)
        this.RectangleGeometryThumbHandler((RectangleGeometry) a.Data);
      else if (a.Data is EllipseGeometry)
        this.EllipseGeometryThumbHandler((EllipseGeometry) a.Data);
      else if (a.Data is GeometryGroup)
        this.GeometryGroupThumbHandler((GeometryGroup) a.Data);
      else if (a.Data is PathGeometry)
      {
        this.PathGeometryThumbHandler((PathGeometry) a.Data);
      }
      else
      {
        if (!(a.Data is CombinedGeometry))
          return;
        this.CombinedGeometryThumbHandler((CombinedGeometry) a.Data);
      }
    }

    protected void CombinedGeometryThumbHandler(CombinedGeometry CG)
    {
      Geometry geometry1 = CG.Geometry1;
      Geometry geometry2 = CG.Geometry2;
      foreach (Geometry geometry in new List<Geometry>()
      {
        geometry1,
        geometry2
      })
      {
        if (geometry is LineGeometry)
          this.LineGeometryThumbHandler((LineGeometry) geometry);
        else if (geometry is RectangleGeometry)
          this.RectangleGeometryThumbHandler((RectangleGeometry) geometry);
        else if (geometry is EllipseGeometry)
          this.EllipseGeometryThumbHandler((EllipseGeometry) geometry);
        else if (geometry is GeometryGroup)
          this.GeometryGroupThumbHandler((GeometryGroup) geometry);
        else if (geometry is PathGeometry)
          this.PathGeometryThumbHandler((PathGeometry) geometry);
        else if (geometry is CombinedGeometry)
          this.CombinedGeometryThumbHandler((CombinedGeometry) geometry);
      }
    }

    protected void EllipseGeometryThumbHandler(EllipseGeometry EG)
    {
      this.PathPoints.Add(new Thumb());
      this.PathPoints.Add(new Thumb());
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 3].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 3].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 2].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 2].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 3].Name = "A" + Convert.ToString(this.PathPoints.Count - 3);
      this.PathPoints[this.PathPoints.Count - 2].Name = "A" + Convert.ToString(this.PathPoints.Count - 2);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
    }

    protected void RectangleGeometryThumbHandler(RectangleGeometry RG)
    {
      this.PathPoints.Add(new Thumb());
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 2].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 2].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 2].Name = "A" + Convert.ToString(this.PathPoints.Count - 2);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
    }

    protected void LineGeometryThumbHandler(LineGeometry LG)
    {
      this.PathPoints.Add(new Thumb());
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 2].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 2].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 2].Name = "A" + Convert.ToString(this.PathPoints.Count - 2);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
    }

    protected void LineSegmentThumbHandler(LineSegment LS)
    {
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
    }

    protected void PolyLineSegmentThumbHandler(PolyLineSegment PLS)
    {
      foreach (Point point in Enumerable.ToList<Point>((IEnumerable<Point>) PLS.Points))
      {
        this.PathPoints.Add(new Thumb());
        this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
        this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
        this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
      }
    }

    protected void PolyBezierSegmentThumbHandler(PolyBezierSegment PBS)
    {
      foreach (Point point in Enumerable.ToList<Point>((IEnumerable<Point>) PBS.Points))
      {
        this.PathPoints.Add(new Thumb());
        this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
        this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
        this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
      }
    }

    protected void PolyQuadraticBezierSegmentThumbHandler(PolyQuadraticBezierSegment PQBS)
    {
      foreach (Point point in Enumerable.ToList<Point>((IEnumerable<Point>) PQBS.Points))
      {
        this.PathPoints.Add(new Thumb());
        this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
        this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
        this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
      }
    }

    protected void ArcSegmentThumbHandler(ArcSegment AS)
    {
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
    }

    protected void BezierSegmentThumbHandler(BezierSegment BS)
    {
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
    }

    protected void QuadraticBezierSegmentThumbHandler(QuadraticBezierSegment QBS)
    {
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
    }

    protected void PathGeometryThumbHandler(PathGeometry PG)
    {
      foreach (PathFigure PF in Enumerable.ToList<PathFigure>((IEnumerable<PathFigure>) PG.Figures))
        this.PathFigureThumbHandler(PF);
    }

    protected void PathFigureThumbHandler(PathFigure PF)
    {
      this.PathPoints.Add(new Thumb());
      this.PathPoints[this.PathPoints.Count - 1].PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.PathPoints_PreviewMouseLeftButtonDown);
      this.PathPoints[this.PathPoints.Count - 1].DragDelta += new DragDeltaEventHandler(this.PathPoints_DragDelta);
      this.PathPoints[this.PathPoints.Count - 1].Name = "A" + Convert.ToString(this.PathPoints.Count - 1);
      foreach (PathSegment pathSegment in Enumerable.ToList<PathSegment>((IEnumerable<PathSegment>) PF.Segments))
      {
        if (pathSegment is LineSegment)
          this.LineSegmentThumbHandler((LineSegment) pathSegment);
        else if (pathSegment is ArcSegment)
          this.ArcSegmentThumbHandler((ArcSegment) pathSegment);
        else if (pathSegment is PolyLineSegment)
          this.PolyLineSegmentThumbHandler((PolyLineSegment) pathSegment);
        else if (pathSegment is BezierSegment)
          this.BezierSegmentThumbHandler((BezierSegment) pathSegment);
        else if (pathSegment is QuadraticBezierSegment)
          this.QuadraticBezierSegmentThumbHandler((QuadraticBezierSegment) pathSegment);
        else if (pathSegment is PolyBezierSegment)
          this.PolyBezierSegmentThumbHandler((PolyBezierSegment) pathSegment);
        else if (pathSegment is PolyQuadraticBezierSegment)
          this.PolyQuadraticBezierSegmentThumbHandler((PolyQuadraticBezierSegment) pathSegment);
      }
    }

    protected void GeometryGroupThumbHandler(GeometryGroup GG)
    {
      foreach (Geometry geometry in Enumerable.ToList<Geometry>((IEnumerable<Geometry>) GG.Children))
      {
        if (geometry is LineGeometry)
          this.LineGeometryThumbHandler((LineGeometry) geometry);
        else if (geometry is RectangleGeometry)
          this.RectangleGeometryThumbHandler((RectangleGeometry) geometry);
        else if (geometry is EllipseGeometry)
          this.EllipseGeometryThumbHandler((EllipseGeometry) geometry);
        else if (geometry is PathGeometry)
          this.PathGeometryThumbHandler((PathGeometry) geometry);
        else if (geometry is GeometryGroup)
          this.GeometryGroupThumbHandler((GeometryGroup) geometry);
        else if (geometry is CombinedGeometry)
          this.CombinedGeometryThumbHandler((CombinedGeometry) geometry);
      }
    }
  }
}
